package com.ss.android.article.base.feature.detail.presenter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.AsyncTask;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.detail.model.CommentWrapper;
import com.ss.android.article.base.feature.model.AppAd;
import com.ss.android.article.base.feature.model.DownloadAppAd;
import com.ss.android.article.browser.R;
import com.ss.android.common.download.DownloadAdapterProxy;
import com.ss.android.common.download.DownloadInfoChangeListener;
import com.ss.android.common.download.DownloadShortInfo;
import com.ss.android.common.util.ToolUtils;
import com.ss.android.image.loader.ImageLoader;
import com.ss.android.newmedia.util.AppUtil;

public final class AppAdViewHolder {
    public final a n;
    protected final Resources k;
    public View a;
    public RatingBar b;
    public TextView c;
    public TextView d;
    public ProgressBar e;
    public View f;
    public TextView g;
    public View h;
    public TextView i;
    public DownloadAppAd l;
    public DownloadShortInfo m;
    protected Context j;
    private View.OnClickListener A;
    private View.OnClickListener B;
    private String o;
    private LinearLayout p;
    private ImageView q;
    private TextView r;
    private TextView s;
    private ViewGroup t;
    private View u;
    private boolean v;
    private int w;
    private ImageLoader x;
    private QueryDownloadInfoTask y;
    private boolean z;

    public AppAdViewHolder(Context arg5, ImageLoader arg6) {
        this.v = false;
        this.n = new a(this);
        this.A = new View.OnClickListener() {

            public final void onClick(View arg7) {
                DownloadShortInfo v3 = null;
                int v5 = 3;
                if (l != null) {
                    if (AppData.getInstance().allowInsideDownloadManager()) {
                        l.a(j, 1, m, n, v5);
                    } else {
                        l.a(j, 1, v3, null, v5);
                    }
                }
            }
        };
        this.B = new View.OnClickListener() {

            public final void onClick(View arg7) {
                DownloadShortInfo v3 = null;
                int v5 = 3;
                int v2 = 2;
                if (l != null) {
                    if (AppData.getInstance().allowInsideDownloadManager()) {
                        l.a(j, v2, m, n, v5);
                    } else {
                        l.a(j, v2, v3, ((DownloadInfoChangeListener) null), v5);
                    }
                }
            }
        };
        this.j = arg5;
        Spipe.inst();
        AppData.getInstance();
        this.k = this.j.getResources();
        TTUtils.getNightModeColorFilter();
        this.x = arg6;
        this.w = ((int) ((((double) this.k.getDisplayMetrics().widthPixels)) * 0.62));
    }

    final void a() {
        int v0 = 10;
        int v4 = View.GONE;
        if (this.l.rate <= v0) {
            v0 = this.l.rate;
        }

        float v0_1 = (((float) v0)) / 2f;
        if (Logger.debug()) {
            Logger.d("ratingBar", "rating = " + v0_1 + " appName = " + this.l.appName);
        }

        if (!android.support.design.a.isTextEmpty(this.l.downloadCount)) {
            UIUtils.setVisibility(this.f, 0);
            UIUtils.setVisibility(this.g, 0);
            UIUtils.setVisibility(this.b, v4);
            UIUtils.setVisibility(this.c, v4);
            this.g.setText(this.l.downloadCount);
        } else {
            UIUtils.setVisibility(this.f, v4);
            if (v0_1 < 0f) {
                this.b.setVisibility(v4);
                this.c.setVisibility(View.VISIBLE);
            } else {
                this.b.setVisibility(View.VISIBLE);
                this.b.setRating(v0_1);
                this.c.setVisibility(v4);
            }
        }
    }

    public final void a(ViewGroup arg4) {
        View v1 = LayoutInflater.from(this.j).inflate(com.ss.android.article.browser.R.layout.comment_ad_app, arg4, false);
        this.a = v1;
        this.p = v1.findViewById(com.ss.android.article.browser.R.id.appad_layout);
        this.t = v1.findViewById(com.ss.android.article.browser.R.id.appicon_area);
        this.q = v1.findViewById(com.ss.android.article.browser.R.id.appicon);
        this.s = v1.findViewById(com.ss.android.article.browser.R.id.appname);
        this.r = v1.findViewById(com.ss.android.article.browser.R.id.appicon_lable_info);
        this.b = v1.findViewById(com.ss.android.article.browser.R.id.star);
        this.c = v1.findViewById(com.ss.android.article.browser.R.id.hot);
        this.d = v1.findViewById(com.ss.android.article.browser.R.id.ad_action_btn);
        this.e = v1.findViewById(com.ss.android.article.browser.R.id.download_progress);
        this.f = v1.findViewById(com.ss.android.article.browser.R.id.download_success);
        this.g = v1.findViewById(com.ss.android.article.browser.R.id.download_success_size);
        this.h = v1.findViewById(com.ss.android.article.browser.R.id.download_text);
        this.i = v1.findViewById(com.ss.android.article.browser.R.id.download_size);
        this.u = v1.findViewById(com.ss.android.article.browser.R.id.divider);
        this.a.setOnClickListener(this.A);
        this.d.setOnClickListener(this.B);
    }

    public final void a(CommentWrapper arg10, boolean arg11) {
        int v0_2;
        int v8 = 2;
        Drawable v5 = null;
        int v6 = View.GONE;
        int v1 = 0;
        if (arg10 != null && arg10.mDownloadAppAd != null) {
            this.o = "comment_ad";
            this.z = arg11;
            DownloadAppAd v0 = arg10.mDownloadAppAd;
            if (v0 != null) {
                this.l = v0;
                if (this.v != AppData.isNightMode()) {
                    this.v = AppData.isNightMode();
                    boolean v2 = this.v;
                    UIUtils.setBackgroundResource(this.a, R.drawable.bg_detail_action);
                    Drawable v0_1 = this.b.getProgressDrawable();
                    if (v2) {
                        ((LayerDrawable) v0_1).getDrawable(0).setColorFilter(this.k.getColor(com.ss.android.article.browser.R.color.e8), PorterDuff.Mode.SRC_IN);
                        ((LayerDrawable) v0_1).getDrawable(1).setColorFilter(this.k.getColor(com.ss.android.article.browser.R.color.e9), PorterDuff.Mode.SRC_IN);
                        ((LayerDrawable) v0_1).getDrawable(v8).setColorFilter(this.k.getColor(com.ss.android.article.browser.R.color.e9), PorterDuff.Mode.SRC_IN);
                    } else {
                        ((LayerDrawable) v0_1).getDrawable(0).clearColorFilter();
                        ((LayerDrawable) v0_1).getDrawable(1).clearColorFilter();
                        ((LayerDrawable) v0_1).getDrawable(v8).clearColorFilter();
                    }

                    this.p.setBackgroundResource(com.ss.android.article.browser.R.drawable.bg_detail_action);
                    this.t.setBackgroundResource(com.ss.android.article.browser.R.drawable.detail_ad_banner_bg);
                    this.r.setBackgroundResource(com.ss.android.article.browser.R.drawable.detail_ad_taobao_image_lable);
                    this.r.setTextColor(ContextCompat.getColorStateList(this.j, R.color.ssxinzi8));
                    this.c.setTextColor(this.k.getColor(com.ss.android.article.browser.R.color.appad_v18_hot_text));
                    this.c.setCompoundDrawablesWithIntrinsicBounds(com.ss.android.article.browser.R.drawable.hoticon_textpage_ad, 0, 0, 0);
                    this.i.setTextColor(this.k.getColor(com.ss.android.article.browser.R.color.appad_v18_download_text));
                    this.g.setTextColor(this.k.getColor(com.ss.android.article.browser.R.color.appad_v18_download_text));
                    this.e.setProgressDrawable(this.k.getDrawable(com.ss.android.article.browser.R.drawable.ad_download_progress_bar_horizontal));
                    this.e.getProgressDrawable().setBounds(this.e.getProgressDrawable().getBounds());
                    if (this.m != null) {
                        switch (this.m.b) {
                            case 4:
                                UIUtils.setMaxLines(this.d, R.drawable.af);
                                this.d.setTextColor(this.k.getColor(R.color.cz));
                                break;
                            case 8:
                                if (ToolUtils.d(this.j, this.m.fileName)) {
                                    UIUtils.setMaxLines(this.d, R.drawable.an);
                                    this.d.setTextColor(this.k.getColor(R.color.d9));
                                } else {
                                    UIUtils.setMaxLines(this.d, R.drawable.ap);
                                    this.d.setTextColor(this.k.getColor(R.color.da));
                                }
                                break;
                            case 1:
                            case 2:
                            case 16:
                                UIUtils.setMaxLines(this.d, R.drawable.ao);
                                this.d.setTextColor(this.k.getColor(R.color.dc));
                                break;
                            default:
                                break;
                        }
                    } else {
                        UIUtils.setMaxLines(this.d, R.drawable.am);
                        this.d.setTextColor(this.k.getColorStateList(com.ss.android.article.browser.R.color.dc));
                    }

                    label_93:
                    this.s.setTextColor(this.k.getColor(com.ss.android.article.browser.R.color.e2));
                    if (this.u == null) {
                    } else {
                        this.u.setBackgroundResource(com.ss.android.article.browser.R.color.ssxinxian1);
                    }
                }
                if (this.l == null) {
                } else {
                    v0 = this.l;
                    if (v0 == null || ((AppAd) v0).imageInfo == null) {
                        v0_2 = 0;
                    } else {
                        if (((AppAd) v0).imageInfo != null && ((AppAd) v0).imageInfo.mWidth > 0) {
                            v0_2 = this.w * ((AppAd) v0).imageInfo.mHeight / ((AppAd) v0).imageInfo.mWidth;
                        } else {
                            v0_2 = 0;
                        }
                    }
                    ImageView v2_1 = this.q;
                    ViewGroup.LayoutParams v3 = v2_1.getLayoutParams();
                    if (v3 != null) {
                        if (v0_2 > 0) {
                            v3.height = v0_2;
                        }

                        v2_1.setLayoutParams(v3);
                    }

                    if (this.x != null && this.l.imageInfo != null) {
                        this.x.loadImage(this.q, this.l.imageInfo, false);
                    }

                    if (this.l == null) {
                        UIUtils.setVisibility(this.p, v6);
                    } else {
                        UIUtils.setVisibility(this.p, 0);
                        if (this.z) {
                            l.trackAdShow(this.j, this.o, this.l, 0);
                        }

                        this.s.setText(this.l.appName);
                        String v0_3 = this.l.appAdSourceLabel;
                        if (android.support.design.a.isTextEmpty(v0_3)) {
                            v0_3 = this.j.getString(com.ss.android.article.browser.R.string.ad_label_info);
                        }

                        this.r.setText(((CharSequence) v0_3));
                        this.d.setCompoundDrawables(v5, v5, v5, v5);
                        this.d.setGravity(17);
                        if (AppData.getInstance().allowInsideDownloadManager()) {
                            if (this.m != null) {
                                Logger.d("AppAdViewHolder bindAppAd unregisterDownloadListener", "downloadShortInfo.id = " + this.m.id + " appName = " + this.l.appName);
                                DownloadAdapterProxy.inst().unregisterDownloadListener(this.j, Long.valueOf(this.m.id), this.n);
                                this.b.setVisibility(v6);
                                this.c.setVisibility(v6);
                            } else {
                                this.a();
                            }
                            if (this.y != null && this.y.getStatus() != AsyncTask.Status.FINISHED) {
                                this.y.cancel(true);
                            }
                            this.y = new QueryDownloadInfoTask(this);
                            com.bytedance.common.utility.b.a.a(this.y, new String[]{this.l.downloadUrl});
                        } else {
                            this.a();
                            this.f.setVisibility(v6);
                            this.h.setVisibility(v6);
                            this.e.setVisibility(v6);
                            UIUtils.setMaxLines(this.d, R.drawable.am);
                        }
                    }
                }
            }
            View v0_4 = this.u;
            if (arg10.e == v8) {
                v1 = 4;
            }
            v0_4.setVisibility(v1);
        }
    }

    final class a implements DownloadInfoChangeListener {
        public final AppAdViewHolder b;
        long mId;

        a(AppAdViewHolder arg1) {
            this.b = arg1;

        }

        public final void setDownloadId(long id) {
            this.mId = id;
        }

        public final void onDownloadProgressChanged(final DownloadShortInfo downloadShortInfo,
                                                    final int arg11,
                                                    final long arg12,
                                                    final long arg14,
                                                    long arg16) {
            this.b.a.post(new Runnable() {

                public final void run() {
                    int v4 = 8;
                    if (downloadShortInfo != null && downloadShortInfo.id == AppAdViewHolder.a.this.mId) {
                        Logger.d("AppAdViewHolder", "mId = " + AppAdViewHolder.a.this.mId
                                + " info.id = " + downloadShortInfo.id
                                + " appName = " + AppAdViewHolder.a.this.b.l.appName);
                        try {
                            AppAdViewHolder.a.this.b.m = downloadShortInfo;
                            String v1 = "";
                            String v0_1 = "";
                            AppAdViewHolder.a.this.b.b.setVisibility(View.GONE);
                            AppAdViewHolder.a.this.b.c.setVisibility(View.GONE);
                            AppAdViewHolder.a.this.b.e.setVisibility(View.VISIBLE);
                            AppAdViewHolder.a.this.b.h.setVisibility(View.VISIBLE);
                            AppAdViewHolder.a.this.b.f.setVisibility(View.GONE);
                            if (arg11 == 1) {
                                v1 = android.support.design.a.a(arg14) + "/" + android.support.design.a.a(arg12);
                                v0_1 = AppAdViewHolder.a.this.b.k.getString(com.ss.android.article.browser.R.string.kf);
                                UIUtils.setMaxLines(AppAdViewHolder.a.this.b.d, R.drawable.af);
                                AppAdViewHolder.a.this.b.d.setTextColor(AppAdViewHolder.a.this.b.k.getColor(com.ss.android.article.browser.R.color.cz));
                            } else if (arg11 == 2) {
                                v1 = android.support.design.a.a(arg14) + "/" + android.support.design.a.a(arg12);
                                v0_1 = AppAdViewHolder.a.this.b.k.getString(com.ss.android.article.browser.R.string.ki);
                                UIUtils.setMaxLines(AppAdViewHolder.a.this.b.d, R.drawable.ag);
                                AppAdViewHolder.a.this.b.d.setTextColor(AppAdViewHolder.a.this.b.k.getColor(com.ss.android.article.browser.R.color.d1));
                            } else if (arg11 == 3) {
                                AppAdViewHolder.a.this.b.f.setVisibility(View.VISIBLE);
                                AppAdViewHolder.a.this.b.h.setVisibility(View.GONE);
                                if (AppAdViewHolder.a.this.b.m.b == 16) {
                                    v1 = android.support.design.a.a(arg12);
                                    v0_1 = AppAdViewHolder.a.this.b.k.getString(com.ss.android.article.browser.R.string.kh);
                                    UIUtils.setMaxLines(AppAdViewHolder.a.this.b.d, R.drawable.ag);
                                    AppAdViewHolder.a.this.b.d.setTextColor(AppAdViewHolder.a.this.b.k.getColor(com.ss.android.article.browser.R.color.d1));
                                } else if (AppAdViewHolder.a.this.b.m.b == v4) {
                                    if (!ToolUtils.d(AppAdViewHolder.a.this.b.j, AppAdViewHolder.a.this.b.m.fileName)) {
                                        v0_1 = AppAdViewHolder.a.this.b.k.getString(com.ss.android.article.browser.R.string.k9);
                                    } else if (!android.support.design.a.isTextEmpty(AppAdViewHolder.a.this.b.l.btnTxt)) {
                                        v0_1 = AppAdViewHolder.a.this.b.l.btnTxt;
                                    } else {
                                        v0_1 = AppAdViewHolder.a.this.b.k.getString(com.ss.android.article.browser.R.string.ke);
                                    }

                                    AppAdViewHolder.a.this.b.d.setTextColor(AppAdViewHolder.a.this.b.k.getColor(com.ss.android.article.browser.R.color.cx));
                                    UIUtils.setMaxLines(AppAdViewHolder.a.this.b.d, R.drawable.ae);
                                    v1 = android.support.design.a.a(arg12);
                                }

                                AppAdViewHolder.a.this.b.e.setVisibility(View.GONE);
                            }

                            if (arg12 > 0) {
                                AppAdViewHolder.a.this.b.e.setProgress(((int) (arg14 * 100 / arg12)));
                            } else {
                                AppAdViewHolder.a.this.b.e.setProgress(0);
                            }

                            if (AppAdViewHolder.a.this.b.h.getVisibility() == View.VISIBLE) {
                                AppAdViewHolder.a.this.b.i.setText(((CharSequence) v1));
                                AppAdViewHolder.a.this.b.d.setText(((CharSequence) v0_1));
                            }

                            if (AppAdViewHolder.a.this.b.f.getVisibility() != View.VISIBLE) {
                                return;
                            }

                            AppAdViewHolder.a.this.b.g.setText(((CharSequence) v1));
                            AppAdViewHolder.a.this.b.d.setText(((CharSequence) v0_1));
                        } catch (Exception v0) {
                            v0.printStackTrace();
                        }
                    }
                }
            });
        }
    }

    final class QueryDownloadInfoTask extends AsyncTask {
        private final AppAdViewHolder a;

        QueryDownloadInfoTask(AppAdViewHolder arg1) {
            this.a = arg1;

        }

        protected final Object doInBackground(Object[] arg4) {
            Object v0_1;
            if (arg4 != null) {
                if (arg4.length > 0 && (TextUtils.isEmpty((CharSequence) arg4[0]))) {
                    v0_1 = null;
                } else if (this.a.j == null) {
                    v0_1 = null;
                } else {
                    v0_1 = DownloadAdapterProxy.inst().queryDownloadInfo(this.a.j, (String) arg4[0]);
                }
            } else {
                v0_1 = null;
            }

            return v0_1;
        }

        protected final void onPostExecute(Object arg10) {
            CharSequence v0_2 = null;
            int v0_4;
            Resources v2;
            TextView v1;
            String v0_3;
            String v7_1 = null;
            CharSequence v7 = null;
            super.onPostExecute(arg10);
            if (!this.isCancelled()) {
                boolean v0 = AppUtil.a(this.a.j, this.a.l.version, this.a.l.pkg);
                try {
                    if ((AppData.getInstance().allowInsideDownloadManager()) && arg10 != null && ((DownloadShortInfo) arg10).id > -1 && !DownloadAdapterProxy.inst().isDownloadSuccessAndFileNotExist(this.a.j, ((DownloadShortInfo) arg10)) && !v0) {
                        if (arg10 != null) {
                            Logger.d("AppAdViewHolder QueryDownloadInfoTask", "result.id = " + ((DownloadShortInfo) arg10).id + " appName = " + this.a.l.appName);
                            DownloadAdapterProxy.inst().registerDownloadListener(this.a.j, Long.valueOf(((DownloadShortInfo) arg10).id), this.a.n, String.valueOf(this.a.l.id), 3, this.a.l.logExtra);
                        }

                        this.a.m = ((DownloadShortInfo) arg10);
                        this.a.b.setVisibility(View.GONE);
                        this.a.c.setVisibility(View.GONE);
                        this.a.e.setVisibility(View.VISIBLE);
                        this.a.f.setVisibility(View.GONE);
                        this.a.h.setVisibility(View.VISIBLE);
                        switch (((DownloadShortInfo) arg10).b) {
                            case 1:
                            case 2: {
                                String v8 = android.support.design.a.a(((DownloadShortInfo) arg10).d) + "/" + android.support.design.a.a(((DownloadShortInfo) arg10).c);
                                v7_1 = this.a.k.getString(com.ss.android.article.browser.R.string.kf);
                                UIUtils.setMaxLines(this.a.d, R.drawable.af);
                                this.a.d.setTextColor(this.a.k.getColor(com.ss.android.article.browser.R.color.cz));
                                DownloadAdapterProxy.inst().registerDownloadListener(this.a.j, Long.valueOf(((DownloadShortInfo) arg10).id), this.a.n, String.valueOf(this.a.l.id), 3, this.a.l.logExtra);
                                v0_2 = v7_1;
                                v7_1 = v8;
                                break;
                            }
                            case 4: {
                                v7_1 = android.support.design.a.a(((DownloadShortInfo) arg10).d) + "/" + android.support.design.a.a(((DownloadShortInfo) arg10).c);
                                v0_2 = this.a.k.getString(com.ss.android.article.browser.R.string.ki);
                                UIUtils.setMaxLines(this.a.d, R.drawable.ag);
                                this.a.d.setTextColor(this.a.k.getColor(com.ss.android.article.browser.R.color.dc));
                                break;
                            }
                            case 8: {
                                if (ToolUtils.d(this.a.j, ((DownloadShortInfo) arg10).fileName)) {
                                    v0_2 = this.a.k.getString(com.ss.android.article.browser.R.string.ke);
                                } else {
                                    v0_2 = this.a.k.getString(com.ss.android.article.browser.R.string.k9);
                                }
                                this.a.d.setTextColor(this.a.k.getColor(com.ss.android.article.browser.R.color.cx));
                                UIUtils.setMaxLines(this.a.d, R.drawable.ae);
                                this.a.f.setVisibility(View.VISIBLE);
                                this.a.h.setVisibility(View.GONE);
                                v7_1 = android.support.design.a.a(((DownloadShortInfo) arg10).d);
                                this.a.e.setVisibility(View.GONE);
                                break;
                            }
                            case 16: {
                                this.a.f.setVisibility(View.VISIBLE);
                                this.a.h.setVisibility(View.GONE);
                                v7_1 = android.support.design.a.a(((DownloadShortInfo) arg10).c);
                                v0_2 = this.a.k.getString(com.ss.android.article.browser.R.string.kh);
                                this.a.e.setVisibility(View.GONE);
                                UIUtils.setMaxLines(this.a.d, R.drawable.ag);
                                this.a.d.setTextColor(this.a.k.getColor(com.ss.android.article.browser.R.color.d1));
                                break;
                            }
                            default: {
                                v0_2 = v7;
                                break;
                            }
                        }
                        if (((DownloadShortInfo) arg10).c > 0) {
                            this.a.e.setProgress(((int) (((DownloadShortInfo) arg10).d * 100 / ((DownloadShortInfo) arg10).c)));
                        } else {
                            this.a.e.setProgress(0);
                        }

                        if (this.a.h.getVisibility() == View.VISIBLE) {
                            this.a.i.setText(v7_1);
                            this.a.d.setText(v0_2);
                        }

                        if (this.a.f.getVisibility() != View.VISIBLE) {
                            return;
                        }

                        this.a.g.setText(((CharSequence) v7_1));
                        this.a.d.setText(v0_2);
                        return;
                    }
                    if (this.a.m != null) {
                        DownloadAdapterProxy.inst().unregisterDownloadListener(this.a.j, Long.valueOf(this.a.m.id), this.a.n);
                    }

                    this.a.m = null;
                    if (android.support.design.a.isTextEmpty(this.a.l.btnTxt)) {
                        v1 = this.a.d;
                        v2 = this.a.k;
                        v0_4 = v0 ? R.string.kj : R.string.ka;
                        v1.setText(v2.getString(v0_4));
                    } else {
                        this.a.d.setText(this.a.l.btnTxt);
                    }

                    this.a.d.setBackgroundResource(com.ss.android.article.browser.R.drawable.ad_action_btn_begin_bg);
                    this.a.d.setTextColor(this.a.k.getColor(com.ss.android.article.browser.R.color.ad_action_btn_begin_text_color));
                    this.a.e.setVisibility(View.GONE);
                    this.a.h.setVisibility(View.GONE);
                    this.a.f.setVisibility(View.GONE);
                    this.a.a();
                    return;
                } catch (Exception v0_1) {
                    v0_1.printStackTrace();
                }
            }
        }
    }
}

